<?php
####用户账户对象
    $this->load->model('Userfee');
    $userfee_obj = new Userfee();
####用户账户对象
    if( isset($article->ad) && !empty($article->ad) ){
        $tmp_ids = $article->ad;
        $ad = $this->db->where("position","index_float")->where_in("id",$tmp_ids)->from('ad')->get()->row();
    }else{
        if( isset($this->user->id) ){
            $user_id = $this->user->id;
        }else{
            $user_id = -1;
        }
        //文章累加
        if( $user_id>0 && isset($article->id)){
            $this->load->model('Articleaccount');
            $articleaccount = new Articleaccount();
            $articleaccount->add_view($article->id,$user_id);
            $userfee_obj->change($article->user_id,0.01,"$user_id 看过您的文章");
        }
        //找到有余额的用户发布的广告
        $sql = "
            select * from wd_ad where
             position = 'index_float'
             AND
             user_id in (
              select wd_user.id from wd_user join wd_user_fee on wd_user.id = wd_user_fee.user_id
              where fee>%s
             )
             AND 
             id NOT IN
             (
                select ad_id from  wd_ad_account
                where user_id = %s
             )
             ";
        $sql = sprintf($sql,0.5,$user_id);
        $ad = $this->db->query($sql)->row();
    }
    //广告计费
    if( $user_id>0 && !empty($ad)) {
        $this->load->model('Adaccount');
        $adaccount = new Adaccount();
        $adaccount->add_view($ad->id, $user_id);
        $userfee_obj->change($ad->user_id, -0.1, "$user_id 看过您的广告");
    }
    //显示广告
    if(empty($ad)){
        $sql = " select * from wd_ad where
             position = 'index_float' ORDER BY RAND() ";
        $ad = $this->db->query($sql)->row();
    }
?>

<div style="position: fixed;top:0;left:0;width: 100%;height: 100%;background: #CCC;z-index: 999;" id="ad_position_1">
    <div
        style="position: fixed;top:1rem;right: 1rem;"
        onclick=" $('#ad_position_1').hide() ">关闭</div>
    <img
            onclick="location.href='<?php t_(isset($ad->url)?$ad->url:'')?>'"
            style="width: 100%;height: 100%;"
            src="<?php t_(isset($ad->pic)?$ad->pic:'')?>"  alt="">
</div>
<script>
    setTimeout(function(){
        $("#ad_position_1").hide();
    },1000*10);
    //去除作者
    $("#meta_content").html("\
        <span class='rich_media_meta rich_media_meta_nickname' >\
        <a href=''>赚钱平台</a>\
        </span>\
    ");
    //去除阅读原文
    $(".rich_media_tool").html('');
</script>


<?php
unset($ad);
?>